# -*- coding: utf-8 -*-

from server.controller import app
from server.controller import current_app
from server.controller import render_template
from server.controller import session


@app.route('/')
def index():
    return render_template('/index.html')


@app.route('/favicon.ico')
def get_favicon():
    return current_app.send_static_file('image/favicon.ico')


@app.route('/menu', methods=['GET'])
def get_menu():
    response = {}
    data = {}

    if 'user' not in session:
        response['code'] = 1
        response['msg'] = "用户未登录"
        response['data'] = data
    else:
        userSession = session.get('user')

        home = {"name": "home",
                "title": "主页",
                "icon": "layui-icon-home",
                "jump": "equipment/list"}

        configuration = {"title": "配置表",
                         "icon": "layui-icon-template",
                         "list": [{
                             "name": "create",
                             "title": "创建配置表",
                             "jump": "home/homepage1"
                         },
                             {
                                 "name": "configuration",
                                 "title": "已有配置表",
                                 "jump": "home/homepage2"
                             }]
                         }

        equipment = {"title": "管理硬件列表",
                     "icon": "layui-icon-component",
                     "list": [{
                         "name": "operation",
                         "title": "管理设备",
                         "jump": "equipment/equipmentlist"
                        },
                         {
                             "name": "invalid",
                             "title": "失效设备列表",
                             "jump": "equipment/invalidlist"
                         }]
                     }

        administration = {"name": "administration",
                          "title": "管理系统用户",
                          "icon": "layui-icon-user",
                          "jump": "manageusers/list"}

        response['code'] = 0
        response['msg'] = ""

        if userSession['role'] == "1":
            data = [home, configuration, equipment, administration]
        elif userSession['role'] == "2":
            data = [home, configuration, equipment]
        else:
            data = [home, configuration]

        response['data'] = data

    return response
